import axios from "axios";
import Vue from "vue";
import Vuex from "vuex";

// 挂载路由
Vue.use(Vuex)

// 实例化
export default new Vuex.Store({
  state: {
    class: "小小",
    userInfo: {
      name: 'tom',
      skills: ['抖音', 'B站', '美团'],
      address: '武汉黑马',
      logo: 'https://vuejs.org/images/logo.svg'
      //  https://www.runoob.com/wp-content/uploads/2016/02/react.png
    },
    bookk: []
  },
  // mutations改变state里面的值
  // 1.1通过commit修改mutations里面的值,然后再改变state
  mutations: {
    set(state, newlogo) {
      state.userInfo.logo = newlogo
    },
    getting(state, newbookk) {
      state.bookk = newbookk
    }
  },
  //   发起异步请求,通过修改mutations来改变state
  actions: {
    //   {commit}es6新增,对象解构
    async setFn({
      commit
    }) {
      let res = await axios({
        url: "https://www.fastmock.site/mock/37d3b9f13a48d528a9339fbed1b81bd5/book/api/books",
      })
    //   console.log(res);
      //   把值通过commit传给mutations,然后间接改变state
      commit('getting', res.data.data)

    }
  }

})
